package siam.ui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.Border;
/**
 * Class which represent a case on board . It can contains a piece
 * @author Motassim Saad
 *
 */
public class SiamCase extends JLabel implements ICase {
	/**
	 * Piece on this case
	 */
	private Piece piece;
	private final EventControler controler = EventControler.instance;
	/**
	 * state if the jlabel is activate
	 */
	private boolean activate;
	private int row;
	private int column;

	/**
	 * Constructor  {@link SiamCase} 
	 * @param i , row
	 * @param j , column
	 */
	public SiamCase(int i, int j) {
		this();
		this.row = i;
		this.column = j;

	}

	/**
	 * Create the panel.
	 */
	public SiamCase() {
		setBorder(UIManager.getBorder("InternalFrame.border"));
		setForeground(Color.BLACK);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 209, 32, 0 };
		gridBagLayout.rowHeights = new int[] { 32, 0 };
		gridBagLayout.columnWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 1.0, Double.MIN_VALUE };
		setLayout(gridBagLayout);
		this.addMouseListener(new MouseListener() {

			public void mouseReleased(MouseEvent e) {
			}

			public void mousePressed(MouseEvent e) {
			}

			public void mouseExited(MouseEvent e) {
			}

			public void mouseEntered(MouseEvent e) {
			}

			public void mouseClicked(MouseEvent e) {
				update();
			}
		});

	}

	public int getColumn() {
		return column;
	}

	public int getRow() {
		return row;
	}

	private void update() {
		controler.notifyState(this);
	}

	public boolean getActivate() {
		return this.activate;
	}

	

	public Piece getPieces() {
		return piece;
	}

	/**
	 * Return true if the  {@link SiamCase} is putable at the beginig else return false
	 */
	public boolean putableAtBegining() {
		return row == 0 || row == 4 || column == 0 || column == 4;
	}

	/**
	 * Put piece on this case
	 */
	public void put(Piece piece) {
		this.piece = piece;
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		if (this.piece != null) {
			int left, bottom;
			left = UIManager.getBorder("InternalFrame.border").getBorderInsets(
					this).left;
			bottom = UIManager.getBorder("InternalFrame.border")
					.getBorderInsets(this).right;

			g.drawImage(piece.getImage(), left, bottom, this.getWidth() - 2
					* left, this.getHeight() - 2 * bottom, this);
			if (piece.getForce() != null) {
				g.setColor(Color.red);
				g.drawString(piece.getForce().name(), this.getWidth()/2, this.getHeight()/2);
			}

		}
	}

	/**
	 * Get the  {@link Piece} from the  {@link SiamCase} and remove the reference from it
	 */
	public Piece popPiece() {
		Piece p = this.piece;
		this.piece = null;
		return p;
	}

	/**
	 * Return true if the  {@link ICase} in parameter is next to currently  {@link SiamCase}
	 */
	public boolean isNextTo(ICase siamCase) {
		return (Math.abs(column - siamCase.getColumn())
				+ Math.abs(row - siamCase.getRow()) == 1);
	}
	
	/**
	 * Activate the  {@link SiamCase}
	 */
	public void activate() {
		this.activate =true;
		Border border = BorderFactory.createLineBorder(Color.yellow);
		setBorder(border);
		this.repaint();
		
	}

	/**
	 * Desactivate the  {@link SiamCase}
	 */
	public void desactivate() {
		this.activate =false;
		setBorder(UIManager.getBorder("InternalFrame.border"));
		this.repaint();
		
	}

}
